import openai

openai.api_key =  "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"

# response = openai.Completion.create(
#     engine="davinci",
#     prompt="以我的家乡为题写一篇800字的作文",
#     max_tokens=100,
#     stream=True,
#     # model="gpt-3.5-turbo",
#     # messages="以我的家乡为题写一篇800字的作文",
# )
#
# for chunk in response:
#     # 处理每个响应的数据块
#     print(chunk['choices'][0]['text'])


messages = [{"role": "system", "content": "百度公司的未来怎么样"}]

while True:
    message = input("User : ")
    if message:
        messages.append(
            {"role": "user", "content": message},
        )
        chat = openai.ChatCompletion.create(
            stream=True,
            model="gpt-3.5-turbo",
            messages=messages
        )
        for chunk in chat:
            print(1, chunk['choices'])
            print(2, chunk['choices'][0])
            # print(chunk['choices'][0])
            # print(chunk['choices'][0])
            print(3, type(chunk['choices'][0]))
            print(4, dir(chunk['choices'][0]))
            print('---------------')
            print(5, chunk['choices'][0]['delta'])
            if chunk['choices'][0]['delta']:
                print(5, chunk['choices'][0]['delta']['content'])

    # reply = chat.choices[0].message.content
    # print(f"ChatGPT: {reply}")
    # messages.append({"role": "assistant", "content": reply})
